import { createStore } from 'vuex'
import router from '@/router/index.js'
import { ElNotification } from "element-plus";
import axios from '@/untils/http.js';
import createPersistedState from "vuex-persistedstate"

export default createStore({
  state: {
    tagArr: [{ path: '/index', name: '首页', hidden: true }],
    isCollapse: false,
    menu: [],
    username: '001',
    includeList: [],
    switch: true
  },
  mutations: {
    // 设置tag标签
    setTag(state, data) {
      state.tagArr = data;
    },
    // 菜单收缩/展开
    changeCollapse(state, data) {
      state.isCollapse = data;
    },
    // 设置菜单数据
    setMenu(state, data) {
      state.menu = data;
    },
    // 设置用户名
    setUsername(state, data) {
      state.username = data;
    },
    // 设置缓存路由
    add_include_component(state, data) {
      state.includeList = data;
    }
  },
  actions: {
    // 获取菜单的接口
    get_menu({commit}) {
      console.log("获取菜单接口要触发。。。。")
      return new Promise((resolve, reject) => {
        axios.get("/api/middle/oa/get_navigation/")
          .then((res) => {
            console.log(res)
            if (res.data.errno == 0) {
              let menu=res.data.data;
              commit("setMenu", menu);
              console.log("菜单存到vuex里")
              resolve(menu);
            } else {
              ElNotification.error(res.data.errmsg);
              // 自动退出登录
              localStorage.clear();
              sessionStorage.clear();
              router.replace("/login");
              reject(res);
            }
          })
          .catch((error) => {
            reject(error);
            ElNotification.error(error);
          });
      })
    },
    // 动态获取菜单，根据后台的path直接对应组件文件位置
    addUserMenus({ commit }, menus) {
      return new Promise((resolve, reject) => {
         console.log("接收到菜单的数据", menus)
        let old_path = router.options.routes;
        // 根据文件位置寻找指定文件
        function _import(file) {
          return () => import('@/views' + file + '.vue')
        }
        menus.forEach(v => {
          v['component'] = () => import('@/views/Layout.vue')
          if (v.children.length) {
            let item = v.children;
            item.forEach((it) => {
              let end = "";
              if (it.path.charAt(it.path.length - 1) == '/') {
                end = it.path.substr(0, it.path.length - 1)
              } else {
                end = it.path
              }
              // it.component=()=>import(`@/views${end}.vue`)
              it.component = _import(end)
            })
          }
        })
        let tempPath = [...old_path, ...menus];
        tempPath.push({
          path: '/:pathMatch(.*)*',
          hidden:true,
          name: "404",
          component: () => import('@/views/nofond.vue')
        })
        console.log(tempPath)
        // 添加授权路由页面
        tempPath.forEach((v) => {
          router.addRoute(v)
        })
        resolve(tempPath);
      })

    }
  },
  plugins: [createPersistedState({
      storage:window.sessionStorage,
          reducer(val)  {
              return {
                  // 只储存state中的assessmentData
              tagArr: val.tagArr
     
              }
     
          }
     
      })],
  modules: {
  }
 
})
